home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 October: Mac OS SDK / Dev.CD Oct 97 SDK1.toast / Development Kits (Disc 1) / Interfaces&Libraries / Universal / Interfaces / PInterfaces / DigitalSignature.p < prev    next >
Encoding:
Text File  |  1997-08-12  |  16.5 KB  |  383 lines  |  [TEXT/MPS ]

  1. {
  2.      File:        DigitalSignature.p
  3.  
  4.      Contains:    Digital Signature Interfaces.
  5.  
  6.      Version:    Technology:    AOCE toolbox 1.02
  7.                  Release:    Universal Interfaces 3.0.1
  8.  
  9.      Copyright:    © 1994-1997 by Apple Computer, Inc., all rights reserved.
  10.  
  11.      Bugs?:        Please include the the file and version information (from above) with
  12.                  the problem description.  Developers belonging to one of the Apple
  13.                  developer programs can submit bug reports to:
  14.  
  15.                      devsupport@apple.com
  16.  
  17. }
  18. {$IFC UNDEFINED UsingIncludes}
  19. {$SETC UsingIncludes := 0}
  20. {$ENDC}
  21.  
  22. {$IFC NOT UsingIncludes}
  23.  UNIT DigitalSignature;
  24.  INTERFACE
  25. {$ENDC}
  26.  
  27. {$IFC UNDEFINED __DIGITALSIGNATURE__}
  28. {$SETC __DIGITALSIGNATURE__ := 1}
  29.  
  30. {$I+}
  31. {$SETC DigitalSignatureIncludes := UsingIncludes}
  32. {$SETC UsingIncludes := 1}
  33.  
  34. {$IFC UNDEFINED __TYPES__}
  35. {$I Types.p}
  36. {$ENDC}
  37. {$IFC UNDEFINED __MIXEDMODE__}
  38. {$I MixedMode.p}
  39. {$ENDC}
  40. {$IFC UNDEFINED __FILES__}
  41. {$I Files.p}
  42. {$ENDC}
  43.  
  44.  
  45. {$PUSH}
  46. {$ALIGN MAC68K}
  47. {$LibExport+}
  48.  
  49. { values of SIGNameAttributeType }
  50.  
  51. CONST
  52.     kSIGCountryCode                = 0;
  53.     kSIGOrganization            = 1;
  54.     kSIGStreetAddress            = 2;
  55.     kSIGState                    = 3;
  56.     kSIGLocality                = 4;
  57.     kSIGCommonName                = 5;
  58.     kSIGTitle                    = 6;
  59.     kSIGOrganizationUnit        = 7;
  60.     kSIGPostalCode                = 8;
  61.  
  62.  
  63. TYPE
  64.     SIGNameAttributeType                = INTEGER;
  65. Certificate status codes returned in SIGCertInfo or SIGSignerInfo from
  66. either SIGGetCertInfo or SIGGetSignerInfo respectively. kSIGValid means that
  67. the certificate is currently valid. kSIGPending means the certificate is
  68. currently not valid - but will be.  kSIGExpired means the certificate has
  69. expired. A time is always associated with a SIGCertStatus.  In each case the
  70. time has a specific interpretation.  When the status is kSIGValid the time is
  71. when the certificate will expire. When the status is kSIGPending the time is
  72. when the certificate will become valid. When the status is kSIGExpired the time
  73. is when the certificate expired. In the SIGCertInfo structure, the startDate
  74. and endDate fields hold the appropriate date information.  In the SIGSignerInfo
  75. structure, this information is provided in the certSetStatusTime field. In the
  76. SIGSignerInfo struct, the status time is actually represented by the SIGSignatureStatus
  77. field which can contain any of the types below. NOTE: The only time you will get 
  78. a kSIGInvalid status is when it pertains to a SIGSignatureStatus field and only when
  79. you get a signature that was created after the certificates expiration date, something
  80. we are not allowing on the Mac but that may not be restricted on other platforms. Also, 
  81. it will not be possible to get a kSIGPending value for SIGSignatureStatus on the Mac but
  82. possibly allowed by other platforms.
  83. }
  84. { Values for SIGCertStatus or SIGSignatureStatus }
  85.  
  86. CONST
  87.     kSIGValid                    = 0;                            {  possible for either a SIGCertStatus or SIGSignatureStatus  }
  88.     kSIGPending                    = 1;                            {  possible for either a SIGCertStatus or SIGSignatureStatus  }
  89.     kSIGExpired                    = 2;                            {  possible for either a SIGCertStatus or SIGSignatureStatus * possible only for a SIGSignatureStatus  }
  90.     kSIGInvalid                    = 3;
  91.  
  92.  
  93. TYPE
  94.     SIGCertStatus                        = INTEGER;
  95.     SIGSignatureStatus                    = INTEGER;
  96. { Number of bytes needed for a digest record when using SIGDigest }
  97.  
  98. CONST
  99.     kSIGDigestSize                = 16;
  100.  
  101.  
  102. TYPE
  103.     SIGDigestData                        = PACKED ARRAY [0..15] OF Byte;
  104.     SIGDigestDataPtr                    = ^Byte;
  105.     SIGCertInfoPtr = ^SIGCertInfo;
  106.     SIGCertInfo = RECORD
  107.         startDate:                LONGINT;                                {  cert start validity date  }
  108.         endDate:                LONGINT;                                {  cert end validity date  }
  109.         certStatus:                SIGCertStatus;                            {  see comment on SIGCertStatus for definition  }
  110.         certAttributeCount:        LONGINT;                                {  number of name attributes in this cert  }
  111.         issuerAttributeCount:    LONGINT;                                {  number of name attributes in this certs issuer  }
  112.         serialNumber:            Str255;                                    {  cert serial number  }
  113.     END;
  114.  
  115.     SIGSignerInfoPtr = ^SIGSignerInfo;
  116.     SIGSignerInfo = RECORD
  117.         signingTime:            LONGINT;                                {  time of signing  }
  118.         certCount:                LONGINT;                                {  number of certificates in the cert set  }
  119.         certSetStatusTime:        LONGINT;                                {  Worst cert status time. See comment on SIGCertStatus for definition  }
  120.         signatureStatus:        SIGSignatureStatus;                        {  The status of the signature. See comment on SIGCertStatus for definition }
  121.     END;
  122.  
  123.     SIGNameAttributesInfoPtr = ^SIGNameAttributesInfo;
  124.     SIGNameAttributesInfo = RECORD
  125.         onNewLevel:                BOOLEAN;
  126.         filler1:                BOOLEAN;
  127.         attributeType:            SIGNameAttributeType;
  128.         attributeScript:        ScriptCode;
  129.         attribute:                Str255;
  130.     END;
  131.  
  132.     SIGContextPtr                        = Ptr;
  133.     SIGSignaturePtr                        = Ptr;
  134. {
  135. Certificates are always in order. That is, the signers cert is always 0, the
  136. issuer of the signers cert is always 1 etc… to the number of certificates-1.
  137. You can use this constant for readability in your code.
  138. }
  139.  
  140. CONST
  141.     kSIGSignerCertIndex            = 0;
  142.  
  143. {
  144. Call back procedure supplied by developer, return false to cancel the current
  145. process.
  146. }
  147.  
  148. TYPE
  149.     SIGStatusProcPtr = ProcPtr;  { FUNCTION SIGStatus: BOOLEAN; }
  150.  
  151.     SIGStatusUPP = UniversalProcPtr;
  152.  
  153. CONST
  154.     uppSIGStatusProcInfo = $00000010;
  155.  
  156. FUNCTION NewSIGStatusProc(userRoutine: SIGStatusProcPtr): SIGStatusUPP;
  157.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  158.     INLINE $2E9F;
  159.     {$ENDC}
  160.  
  161. FUNCTION CallSIGStatusProc(userRoutine: SIGStatusUPP): BOOLEAN;
  162.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  163.     INLINE $205F, $4E90;
  164.     {$ENDC}
  165. {
  166. Resource id's of standard signature icon suite, all sizes and colors are available.
  167. }
  168.  
  169. CONST
  170.     kSIGSignatureIconResID        = -16797;
  171.     kSIGValidSignatureIconResID    = -16799;
  172.     kSIGInvalidSignatureIconResID = -16798;
  173.  
  174. { ——————————————————————————————— CONTEXT CALLS ——————————————————————————————— 
  175. To use the Digital Signature toolbox you will need a SIGContextPtr.  To create
  176. a SIGContextPtr you simply call SIGNewContext and it will create and initialize
  177. a context for you.  To free the memory occupied by the context and invalidate
  178. its internal data, call SIGDisposeContext. An initialized context has no notion
  179. of the type of operation it will be performing however, once you call
  180. SIGSignPrepare SIGVerifyPrepare, or SIGDigestPrepare, the contexts operation
  181. type is set and to switch  to another type of operation will require creating a
  182. new context. Be sure to pass the same context to corresponding toolbox calls
  183. (ie SIGSignPrepare, SIGProcessData, SIGSign)  in other words mixing lets say
  184. signing and verify calls with the same context is not allowed.
  185. }
  186. FUNCTION SIGNewContext(VAR context: SIGContextPtr): OSErr;
  187.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  188.     INLINE $203C, $0002, $076C, $AA5D;
  189.     {$ENDC}
  190. FUNCTION SIGDisposeContext(context: SIGContextPtr): OSErr;
  191.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  192.     INLINE $203C, $0002, $076D, $AA5D;
  193.     {$ENDC}
  194.  
  195. { ——————————————————————————————— SIGNING CALLS ——————————————————————————————— 
  196. Once you have created a SIGContextPtr, you create a signature by calling
  197. SIGSignPrepare once, followed by n calls to SIGProcessData, followed by one call
  198. toRcpt SIGSign. To create another signature on different data but for the same
  199. signer, don't dispose of the context and call SIGProcessData for the new data
  200. followed by a call SIGSign again. In this case the signer will not be prompted
  201. for their signer and password again as it was already provided.  Once you call
  202. SIGDisposeContext, all signer information will be cleared out of the context and
  203. the signer will be re-prompted.  The signer file FSSpecPtr should be set to nil
  204. if you want the toolbox to use the last signer by default or prompt for a signer
  205. if none exists.  The prompt parameter can be used to pass a string to be displayed
  206. in the dialog that prompts the user for their password.  If the substring "^1"
  207. (without the quotes) is in the prompt string, then the toolbox will replace it
  208. with the name of the signer from the signer selected by the user.  If an empty
  209. string is passed, the following default string will be sent to the toolbox
  210. "\pSigning as ^1.".  You can call any of the utility routines after SIGSignPrepare
  211. or SIGSign to get information about the signer or certs.
  212. }
  213. FUNCTION SIGSignPrepare(context: SIGContextPtr; {CONST}VAR signerFile: FSSpec; prompt: ConstStr255Param; VAR signatureSize: Size): OSErr;
  214.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  215.     INLINE $203C, $0008, $076E, $AA5D;
  216.     {$ENDC}
  217. FUNCTION SIGSign(context: SIGContextPtr; signature: SIGSignaturePtr; statusProc: SIGStatusProcPtr): OSErr;
  218.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  219.     INLINE $203C, $0006, $076F, $AA5D;
  220.     {$ENDC}
  221.  
  222. { ——————————————————————————————— VERIFYING CALLS ——————————————————————————————— 
  223. Once you have created a SIGContextPtr, you verify a signature by calling
  224. SIGVerifyPrepare  once, followed by n calls to SIGProcessData, followed by one
  225. call to SIGVerify. Check the return code from SIGVerify to see if the signature
  226. verified or not (noErr is returned on  success otherwise the appropriate error
  227. code).  Upon successfull verification, you can call any of the utility routines
  228. toRcpt find out who signed the data.
  229. }
  230. FUNCTION SIGVerifyPrepare(context: SIGContextPtr; signature: SIGSignaturePtr; signatureSize: Size; statusProc: SIGStatusProcPtr): OSErr;
  231.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  232.     INLINE $203C, $0008, $0770, $AA5D;
  233.     {$ENDC}
  234. FUNCTION SIGVerify(context: SIGContextPtr): OSErr;
  235.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  236.     INLINE $203C, $0002, $0771, $AA5D;
  237.     {$ENDC}
  238. { —————————————————————————————— DIGESTING CALLS —————————————————————————————— 
  239. Once you have created a SIGContextPtr, you create a digest by calling
  240. SIGDigestPrepare once, followed by n calls to SIGProcessData, followed by one
  241. call to SIGDigest.  You can dispose of the context after SIGDigest as the
  242. SIGDigestData does not reference back into it.  SIGDigest returns the digest in
  243. digest.
  244. }
  245. FUNCTION SIGDigestPrepare(context: SIGContextPtr): OSErr;
  246.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  247.     INLINE $203C, $0002, $0772, $AA5D;
  248.     {$ENDC}
  249. FUNCTION SIGDigest(context: SIGContextPtr; VAR digest: SIGDigestData): OSErr;
  250.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  251.     INLINE $203C, $0004, $0773, $AA5D;
  252.     {$ENDC}
  253.  
  254. { —————————————————————————————— PROCESSING DATA —————————————————————————————— 
  255. To process data during a digest, sign, or verify operation call SIGProcessData
  256. as many times as necessary and with any sized blocks of data.  The data needs to
  257. be processed in the same order during corresponding sign and verify operations
  258. but does not need to be processed in the same sized chunks (i.e., the toolbox
  259. just sees it as a continuous bit stream).
  260. }
  261. FUNCTION SIGProcessData(context: SIGContextPtr; data: UNIV Ptr; dataSize: Size): OSErr;
  262.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  263.     INLINE $203C, $0006, $0774, $AA5D;
  264.     {$ENDC}
  265.  
  266. { ——————————————————————————————— UTILITY CALLS ——————————————————————————————— 
  267. Given a context that has successfully performed a verification SIGShowSigner
  268. will  display a modal dialog with the entire distinguished name of the person
  269. who signed the data. the prompt (if supplied) will appear at the top of the
  270. dialog.  If no prompt is specified, the default prompt "\pVerification
  271. Successfull." will appear.
  272.  
  273. Given a context that has been populated by calling SIGSignPrepare, SIGSign or a
  274. successful SIGVerify, you can make the remaining utility calls:
  275.  
  276. SIGGetSignerInfo will return the SignerInfo record.  The certCount can be used
  277. toRcpt index into the certificate set when calling SIGGetCertInfo,
  278. SIGGetCertNameAttributes or SIGGetCertIssuerNameAttributes. The signingTime is
  279. only defined if the call is made after SIGSign  or SIGVerify. The certSetStatus
  280. will tell you the best status of the entire certificate set while
  281. certSetStatusTime will correspond to the time associated with that status (see
  282. definitions above).
  283.  
  284. SIGGetCertInfo will return the SIGCertInfo record when given a valid index into
  285. the cert set in  certIndex.  Note: The cert at index kSIGSignerCertIndex is
  286. always the signers certificate.  The  serial number, start date and end date
  287. are there should you wish to display that info.  The  certAttributeCount and
  288. issuerAttributeCount provide the number of parts in the name of that certificate
  289. or that certificates issuer respectively.  You use these numbers to index into
  290. either SIGGetCertNameAttributes or SIGGetCertIssuerNameAttributes to retrieve
  291. the name. The certStatus will tell you the status of the certificate while
  292. certStatusTime will correspond to the time associated with that status (see
  293. definitions above).
  294.  
  295. SIGGetCertNameAttributes and SIGGetCertIssuerNameAttributes return name parts
  296. of the certificate at  certIndex and attributeIndex.  The newLevel return value
  297. tells you wether the name attribute returned is at the same level in the name
  298. hierarchy as the previous attribute.  The type return value tells you  the type
  299. of attribute returned. nameAttribute is the actual string containing the name
  300. attribute.   So, if you wanted to display the entire distinguished name of the
  301. person who's signature was just validated you could do something like this;
  302.  
  303.     (…… variable declarations and verification code would preceed this sample ……)
  304.  
  305.     error = SIGGetCertInfo(verifyContext, kSIGSignerCertIndex, &certInfo);
  306.     HandleErr(error);
  307.  
  308.     for (i = 0; i <= certInfo.certAttributeCount-1; i++)
  309.         (
  310.         error = SIGGetCertNameAttributes(
  311.             verifyContext, kSIGSignerCertIndex, i, &newLevel, &type, theAttribute);
  312.         HandleErr(error);
  313.         DisplayNamePart(theAttribute, type, newLevel);
  314.         )
  315. }
  316. FUNCTION SIGShowSigner(context: SIGContextPtr; prompt: ConstStr255Param): OSErr;
  317.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  318.     INLINE $203C, $0004, $0775, $AA5D;
  319.     {$ENDC}
  320. FUNCTION SIGGetSignerInfo(context: SIGContextPtr; VAR signerInfo: SIGSignerInfo): OSErr;
  321.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  322.     INLINE $203C, $0004, $0776, $AA5D;
  323.     {$ENDC}
  324. FUNCTION SIGGetCertInfo(context: SIGContextPtr; certIndex: LONGINT; VAR certInfo: SIGCertInfo): OSErr;
  325.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  326.     INLINE $203C, $0006, $0777, $AA5D;
  327.     {$ENDC}
  328. FUNCTION SIGGetCertNameAttributes(context: SIGContextPtr; certIndex: LONGINT; attributeIndex: LONGINT; VAR attributeInfo: SIGNameAttributesInfo): OSErr;
  329.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  330.     INLINE $203C, $0008, $0778, $AA5D;
  331.     {$ENDC}
  332. FUNCTION SIGGetCertIssuerNameAttributes(context: SIGContextPtr; certIndex: LONGINT; attributeIndex: LONGINT; VAR attributeInfo: SIGNameAttributesInfo): OSErr;
  333.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  334.     INLINE $203C, $0008, $0779, $AA5D;
  335.     {$ENDC}
  336.  
  337.  
  338. { ——————————————————————————— FILE SIGN & VERIFY CALLS —————————————————————————— 
  339. These calls allow you to detect the presence of a standard signtaure in a file as 
  340. well as sign and verify files in a standard way.  An example of this is the Finder, 
  341. which uses these calls to allow the user to "drop sign" a file.
  342.  
  343. To detect if a file is signed in the standard way, pass the FSSpec of the file to SIGFileIsSigned.
  344. A result of noErr means the file is in fact signed, otherwise, a kSIGNoSignature error will
  345. be returned.
  346.  
  347. Once you have created a SIGContextPtr, you can make calls to either sign or verify a file in
  348. a standard way: 
  349.  
  350. To sign a file, call SIGSignPrepare followed by 'n' number of calls to SIGSignFile,
  351. passing it the file spec for each file you wish to sign in turn.  You supply the context, the signature 
  352. size that was returned from SIGSignPrepare and an optional call back proc.  The call will take care of all
  353. the processing of data and affixing the signature to the file. If a signature already exists in the file, 
  354. it is replaced with the newly created signature.
  355.  
  356. To verify a file that was signed using SIGSignFile, call SIGVerifyFile passing it a new context and 
  357. the file spec.  Once this call has completed, if the verification is successfull, you can pass the context 
  358. to SIGShowSigner to display the name of the person who signed the file.
  359. }
  360. FUNCTION SIGFileIsSigned({CONST}VAR fileSpec: FSSpec): OSErr;
  361.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  362.     INLINE $203C, $0002, $09C4, $AA5D;
  363.     {$ENDC}
  364. FUNCTION SIGSignFile(context: SIGContextPtr; signatureSize: Size; {CONST}VAR fileSpec: FSSpec; statusProc: SIGStatusProcPtr): OSErr;
  365.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  366.     INLINE $203C, $0008, $09C5, $AA5D;
  367.     {$ENDC}
  368. FUNCTION SIGVerifyFile(context: SIGContextPtr; {CONST}VAR fileSpec: FSSpec; statusProc: SIGStatusProcPtr): OSErr;
  369.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  370.     INLINE $203C, $0006, $09C6, $AA5D;
  371.     {$ENDC}
  372. {$ALIGN RESET}
  373. {$POP}
  374.  
  375. {$SETC UsingIncludes := DigitalSignatureIncludes}
  376.  
  377. {$ENDC} {__DIGITALSIGNATURE__}
  378.  
  379. {$IFC NOT UsingIncludes}
  380.  END.
  381. {$ENDC}
  382.